/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import Controladores.ControladorDisciplina;
import Controladores.ControladorUsuario;
import Logico.Disciplina;
import Logico.Usuario;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 *
 * @author vladimir
 */
public class DisciplinaGUI extends javax.swing.JPanel {

    /**
     * Creates new form UsuarioGUI
     */
    ControladorDisciplina controladorDisciplina;   
    int idDisciplina;
    String nombre;
    String descripcion;
    String logo;
    List<Disciplina> listaDisciplinas;
    int indice;

    public DisciplinaGUI() {
        initComponents();
        controladorDisciplina = new ControladorDisciplina();
        listaDisciplinas = controladorDisciplina.listardisciplinas();
        idDisciplina = 0;
        indice = 0;
        inicializarTabla();
    }

    void inicializarTabla() {
        tablaDisciplinas.removeAll();
        DefaultTableModel modelo = (DefaultTableModel) tablaDisciplinas.getModel();
        modelo.setRowCount(0);

        for (int i = 0; i < listaDisciplinas.size(); i++) {
            String temp[] = new String[3];

            temp[0] = listaDisciplinas.get(i).getIddisciplina() + "";
            temp[1] = listaDisciplinas.get(i).getNombre() + "";
            temp[2] = listaDisciplinas.get(i).getDescripcion() + "";

            Object nuevo[] = null;
            modelo.addRow(nuevo);
            tablaDisciplinas.setModel(modelo);
            for (int j = 0; j < temp.length; j++) {
                tablaDisciplinas.setValueAt(temp[j], i, j);
            }
        }

        if (listaDisciplinas.size() > 0) {
            indice = 0;
            actualizarFormulario();
        }

    }

    void actualizarFormulario() {
        idDisciplina = listaDisciplinas.get(indice).getIddisciplina();
        campoId.setText(idDisciplina + "");
        nombre = listaDisciplinas.get(indice).getNombre();
        campoNombre.setText(nombre);
        descripcion = listaDisciplinas.get(indice).getDescripcion();
        campoDescripcion.setText(descripcion);
        logo = listaDisciplinas.get(indice).getLogo();
    }

    void limpiarFormulario() {
        idDisciplina = 0;
        campoId.setText(idDisciplina + "");
        nombre = "";
        campoNombre.setText(nombre);
        descripcion = "";
        campoDescripcion.setText(descripcion);
        logo = "";
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        campoDescripcion = new javax.swing.JTextField();
        campoNombre = new javax.swing.JTextField();
        campoId = new javax.swing.JTextField();
        botonGuardar = new javax.swing.JButton();
        botonActualizar = new javax.swing.JButton();
        botonEliminar = new javax.swing.JButton();
        botonBuscar = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaDisciplinas = new javax.swing.JTable();
        botonAnterior = new javax.swing.JButton();
        botonSiguiente = new javax.swing.JButton();
        botonLimpiar = new javax.swing.JButton();
        panelLogo = new javax.swing.JPanel();

        jLabel1.setText("Identificador (*)");

        jLabel2.setText("Nombre");

        jLabel3.setText("Descripción");

        campoId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                campoIdActionPerformed(evt);
            }
        });

        botonGuardar.setText("Guardar");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        botonActualizar.setText("Actualizar");
        botonActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonActualizarActionPerformed(evt);
            }
        });

        botonEliminar.setText("Eliminar");
        botonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEliminarActionPerformed(evt);
            }
        });

        botonBuscar.setText("Buscar");
        botonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBuscarActionPerformed(evt);
            }
        });

        tablaDisciplinas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Identificador", "Nombre", "Descripción"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tablaDisciplinas);

        botonAnterior.setText("<<");
        botonAnterior.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAnteriorActionPerformed(evt);
            }
        });

        botonSiguiente.setText(">>");
        botonSiguiente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonSiguienteActionPerformed(evt);
            }
        });

        botonLimpiar.setText("Limpiar");
        botonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonLimpiarActionPerformed(evt);
            }
        });

        panelLogo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        javax.swing.GroupLayout panelLogoLayout = new javax.swing.GroupLayout(panelLogo);
        panelLogo.setLayout(panelLogoLayout);
        panelLogoLayout.setHorizontalGroup(
            panelLogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );
        panelLogoLayout.setVerticalGroup(
            panelLogoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 0, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(botonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(botonActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(botonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(botonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(botonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel3)
                                    .addComponent(jLabel2)
                                    .addComponent(jLabel1))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(campoDescripcion, javax.swing.GroupLayout.DEFAULT_SIZE, 340, Short.MAX_VALUE)
                                    .addComponent(campoNombre)
                                    .addComponent(campoId))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(botonAnterior)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(botonSiguiente))
                            .addComponent(panelLogo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(campoId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(campoNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(campoDescripcion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(panelLogo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(95, 95, 95)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonGuardar)
                    .addComponent(botonActualizar)
                    .addComponent(botonEliminar)
                    .addComponent(botonBuscar)
                    .addComponent(botonAnterior)
                    .addComponent(botonSiguiente)
                    .addComponent(botonLimpiar))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
        // TODO add your handling code here:
        if (!campoId.getText().equals("")
                && !campoNombre.getText().equals("")
                && !campoDescripcion.getText().equals("")) {
            
            idDisciplina = Integer.parseInt(campoId.getText());
            nombre = campoNombre.getText();
            descripcion = campoDescripcion.getText();
            boolean registrado = controladorDisciplina.guardar(nombre, descripcion, logo);

            if (registrado) {
                JOptionPane.showMessageDialog(null, "Disciplina registrado con exito");
                listaDisciplinas = controladorDisciplina.listardisciplinas();
                inicializarTabla();
            } else {
                JOptionPane.showMessageDialog(null, "Disciplina no se pudo registrar");
            }
        } else {
            JOptionPane.showMessageDialog(null, "Disciplina los datos del formulario");
        }

    }//GEN-LAST:event_botonGuardarActionPerformed

    private void botonActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonActualizarActionPerformed
        // TODO add your handling code here:
        
        if (campoId.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "Los campos obligatorios (*) no deben estar vacios");
                
        } else {
            idDisciplina = Integer.parseInt(campoId.getText());
            nombre = campoNombre.getText();
            descripcion = campoDescripcion.getText();

            boolean actualizado = controladorDisciplina.modificar(idDisciplina, nombre, descripcion, logo);

            if (actualizado) {
                JOptionPane.showMessageDialog(null, "Disciplina actualizada con exito");
            } else {
                JOptionPane.showMessageDialog(null, "Disciplina no pudo ser actualizada");
            }
        }
    }//GEN-LAST:event_botonActualizarActionPerformed

    private void botonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEliminarActionPerformed
        // TODO add your handling code here:
        if(idDisciplina!=0){
        idDisciplina = Integer.parseInt(campoId.getText());
        boolean borrado = controladorDisciplina.borrar(idDisciplina);

        if (borrado) {
            JOptionPane.showMessageDialog(null, "Disciplina eliminada con exito");
            limpiarFormulario();
            listaDisciplinas = controladorDisciplina.listardisciplinas();
            inicializarTabla();
        } else {
            JOptionPane.showMessageDialog(null, "Disciplina no se pudo eliminar");
        }
        }else{
            JOptionPane.showMessageDialog(null, "Ingrese un numero de identificador");
        }
    }//GEN-LAST:event_botonEliminarActionPerformed

    private void botonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBuscarActionPerformed
        // TODO add your handling code here:
        if (idDisciplina != 0) {
            Disciplina d = controladorDisciplina.buscar(idDisciplina);
            if (d != null) {
                listaDisciplinas.removeAll(listaDisciplinas);
                listaDisciplinas.add(d);
                inicializarTabla();
            } else {
                JOptionPane.showMessageDialog(null, "No se encontro la Disciplina " + idDisciplina);
            }
        } else {
            JOptionPane.showMessageDialog(null, "Ingrese un numero de identificador");
        }
    }//GEN-LAST:event_botonBuscarActionPerformed

    private void botonAnteriorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAnteriorActionPerformed
        // TODO add your handling code here:
        if (indice > 0) {
            indice--;
            actualizarFormulario();
        }
    }//GEN-LAST:event_botonAnteriorActionPerformed

    private void botonSiguienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonSiguienteActionPerformed
        // TODO add your handling code here:
        if (indice < listaDisciplinas.size() - 1) {
            indice++;
            actualizarFormulario();
        }
    }//GEN-LAST:event_botonSiguienteActionPerformed

    private void botonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonLimpiarActionPerformed
        // TODO add your handling code here:
        limpiarFormulario();
    }//GEN-LAST:event_botonLimpiarActionPerformed

    private void campoIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_campoIdActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_campoIdActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonActualizar;
    private javax.swing.JButton botonAnterior;
    private javax.swing.JButton botonBuscar;
    private javax.swing.JButton botonEliminar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JButton botonLimpiar;
    private javax.swing.JButton botonSiguiente;
    private javax.swing.JTextField campoDescripcion;
    private javax.swing.JTextField campoId;
    private javax.swing.JTextField campoNombre;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel panelLogo;
    private javax.swing.JTable tablaDisciplinas;
    // End of variables declaration//GEN-END:variables
}
